package com.atguigu.servlet;

import com.atguigu.bean.Order;
import com.atguigu.bean.OrderItem;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Date:2020/9/16
 * Author:ybc
 * Description:
 */
public class OrderManagerServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 查询所有订单信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getAllOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service处理业务逻辑，实现查询所有订单的功能
        List<Order> list = orderService.getAllOrder();
        //将所有的订单信息放在请求域中
        request.setAttribute("list", list);
        //转发到管理端的订单页面order_manager.jsp
        request.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(request, response);
    }

    /**
     * 发货
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String orderId = request.getParameter("orderId");
        //调用service处理业务逻辑，实现发货功能
        orderService.sendOrder(orderId);
        //重定向到查询所有订单信息功能
        response.sendRedirect(request.getContextPath() + "/OrderManagerServlet?method=getAllOrder");
    }

    /**
     * 管理端的订单详情功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String orderId = request.getParameter("orderId");
        //调用service处理业务逻辑，获取订单详情
        List<OrderItem> list = orderService.getDetails(orderId);
        //将订单详情，即订单中所有订单项放在请求域中
        request.setAttribute("list", list);
        //转发到客户端的订单详情页面
        request.getRequestDispatcher("/pages/manager/order_details.jsp").forward(request, response);
    }
}
